package com.liwie.app;

import java.util.Vector;

public class MyApp8 {
    //线程集合
    public static Vector<Runnable> runnables=new Vector<>();
    //线程锁
    public Object lock=new Object();
    //主线程
    public Thread threadMain;
    //业务1线程
    Runnable r1=()->{
        try {
            //主线程加入
            threadMain.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        synchronized (lock){
            for (int i = 1; i <= 10; i++) {
                try {
                    Thread.sleep(200);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName()+":"+i);
                lock.notifyAll();
                try {
                    lock.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
            //线程自己干掉自己
            runnables.remove(myApp8.r1);
            System.out.println(Thread.currentThread().getName()+":线程结束.......................");
        }
    };
    //业务2线程
    Runnable r2=()->{
        try {
            //主线程加入
            threadMain.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        synchronized (lock){
            for (int i = 20; i <=40; i++) {
                try {
                    Thread.sleep(200);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName()+":"+i);
                lock.notifyAll();
                try {
                    lock.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            runnables.remove(myApp8.r2);
            System.out.println(Thread.currentThread().getName()+":线程结束.......................");
        }
    };
    //监听线程（心跳线程）
    Runnable r3=()->{
        try {
            //主线程加入
            threadMain.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        while (true){
            try {
                Thread.sleep(400);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (lock){
             //System.out.println("runnables.size():"+runnables.size());
                //查看集合
                if (runnables.size()==1){
                    lock.notifyAll();
                }else if (runnables.size()==0){
                    break;
                }
            }
        }
    };
    public static MyApp8 myApp8 = new MyApp8();
    public static void main(String[] args) {
        //获取主线程
        myApp8.threadMain=Thread.currentThread();
        //线程准备
        Thread thread1=new Thread(myApp8.r1,"r1");
        Thread thread2=new Thread(myApp8.r2,"r2");
        Thread thread3=new Thread(myApp8.r3,"r3");
        //线程开启【利用主线程join，等待主线程完毕】
        thread1.start();
        thread2.start();
        thread3.start();
        //进入主线程[必须等到这里运行完毕，次线程才能运行]
        runnables.add(myApp8.r1);
        runnables.add(myApp8.r2);
        System.out.println("主线程结束..................");
    }
}
